home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OverlayLayout.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  272 lines

  1. /*
  2.  * @(#)OverlayLayout.java    1.15 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22.  
  23. import java.awt.*;
  24. import java.io.Serializable;
  25.  
  26. /**
  27.  * A layout manager to arrange components over the top
  28.  * of each other.  The requested size of the container
  29.  * will be the largest requested size of the children, 
  30.  * taking alignment needs into consideration.  
  31.  *
  32.  * The alignment is based upon what is needed to properly
  33.  * fit the children in the allocation area.  The children
  34.  * will be placed such that their alignment points are all
  35.  * on top of each other.
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate 
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @version 1.15 04/09/98
  45.  * @author   Timothy Prinzing
  46.  */
  47. public class OverlayLayout implements LayoutManager2,Serializable {
  48.  
  49.     /**
  50.      * Constructs a layout manager that performs overlay
  51.      * arrangment of the children.  The layout manager
  52.      * created is dedicated to the given container.
  53.      *
  54.      * @param target  The container to do layout against.
  55.      */
  56.     public OverlayLayout(Container target) {
  57.     this.target = target;
  58.     }
  59.  
  60.     /**
  61.      * Indicates a child has changed its layout related information,
  62.      * which causes any cached calculations to be flushed.
  63.      *
  64.      * @param target the container
  65.      */
  66.     public void invalidateLayout(Container target) {
  67.     checkContainer(target);
  68.     xChildren = null;
  69.     yChildren = null;
  70.     xTotal = null;
  71.     yTotal = null;
  72.     }
  73.  
  74.     /**
  75.      * Adds the specified component to the layout. Not used by this class.
  76.      *
  77.      * @param name the name of the component
  78.      * @param comp the the component to be added
  79.      */
  80.     public void addLayoutComponent(String name, Component comp) {
  81.     }
  82.  
  83.     /**
  84.      * Removes the specified component from the layout. Not used by
  85.      * this class.  
  86.      *
  87.      * @param comp the component to remove
  88.      */
  89.     public void removeLayoutComponent(Component comp) {
  90.     }
  91.  
  92.     /**
  93.      * Adds the specified component to the layout, using the specified
  94.      * constraint object.
  95.      *
  96.      * @param comp the component to be added
  97.      * @param constraints  where/how the component is added to the layout.
  98.      */
  99.     public void addLayoutComponent(Component comp, Object constraints) {
  100.     }
  101.  
  102.     /**
  103.      * Returns the preferred dimensions for this layout given the components
  104.      * in the specified target container.  Recomputes the layout if it
  105.      * has been invalidated.  Factors in the current inset setting returned
  106.      * by getInsets().
  107.      *
  108.      * @param target the component which needs to be laid out
  109.      * @return a Dimension object containing the preferred dimensions
  110.      * @see target the Container to be laid out
  111.      * @see #minimumLayoutSize
  112.      */
  113.     public Dimension preferredLayoutSize(Container target) {
  114.     checkContainer(target);
  115.     checkRequests();
  116.  
  117.     Dimension size = new Dimension(xTotal.preferred, yTotal.preferred);
  118.     Insets insets = target.getInsets();
  119.     size.width += insets.left + insets.right;
  120.     size.height += insets.top + insets.bottom;
  121.     return size;
  122.     }
  123.  
  124.     /**
  125.      * Returns the minimum dimensions needed to lay out the components
  126.      * contained in the specified target container.  Recomputes the layout
  127.      * if it has been invalidated, and factors in the current inset setting.
  128.      *
  129.      * @param target the component which needs to be laid out 
  130.      * @return a Dimension object containing the minimum dimensions
  131.      * @see #preferredLayoutSize
  132.      */
  133.     public Dimension minimumLayoutSize(Container target) {
  134.     checkContainer(target);
  135.     checkRequests();
  136.  
  137.     Dimension size = new Dimension(xTotal.minimum, yTotal.minimum);
  138.     Insets insets = target.getInsets();
  139.     size.width += insets.left + insets.right;
  140.     size.height += insets.top + insets.bottom;
  141.     return size;
  142.     }
  143.  
  144.     /**
  145.      * Returns the minimum dimensions needed to lay out the components
  146.      * contained in the specified target container.  Recomputes the
  147.      * layout if it has been invalidated, and factors in the inset setting
  148.      * returned by getInset().
  149.      *
  150.      * @param target the component which needs to be laid out 
  151.      * @return a Dimension object containing the maximum dimensions
  152.      * @see #preferredLayoutSize
  153.      */
  154.     public Dimension maximumLayoutSize(Container target) {
  155.     checkContainer(target);
  156.     checkRequests();
  157.  
  158.     Dimension size = new Dimension(xTotal.maximum, yTotal.maximum);
  159.     Insets insets = target.getInsets();
  160.     size.width += insets.left + insets.right;
  161.     size.height += insets.top + insets.bottom;
  162.     return size;
  163.     }
  164.  
  165.     /**
  166.      * Returns the alignment along the x axis for the container.
  167.      * If the major axis of the box is the x axis the default
  168.      * alignment will be returned, otherwise the alignment needed
  169.      * to place the children along the x axis will be returned.
  170.      *
  171.      * @param target the container
  172.      * @return the alignment >= 0.0f && <= 1.0f
  173.      */
  174.     public float getLayoutAlignmentX(Container target) {
  175.     checkContainer(target);
  176.     checkRequests();
  177.     return xTotal.alignment;
  178.     }
  179.  
  180.     /**
  181.      * Returns the alignment along the y axis for the container.
  182.      * If the major axis of the box is the y axis the default
  183.      * alignment will be returned, otherwise the alignment needed
  184.      * to place the children along the y axis will be returned.
  185.      *
  186.      * @param target the container
  187.      * @return the alignment >= 0.0f && <= 1.0f
  188.      */
  189.     public float getLayoutAlignmentY(Container target) {
  190.     checkContainer(target);
  191.     checkRequests();
  192.     return yTotal.alignment;
  193.     }
  194.  
  195.     /**
  196.      * Called by the AWT when the specified container needs to be laid out.
  197.      *
  198.      * @param target  the container to lay out
  199.      *
  200.      * @exception AWTError  if the target isn't the container specified to the
  201.      *                      BoxLayout constructor
  202.      */
  203.     public void layoutContainer(Container target) {
  204.     checkContainer(target);
  205.     checkRequests();
  206.     
  207.     int nChildren = target.getComponentCount();
  208.     int[] xOffsets = new int[nChildren];
  209.     int[] xSpans = new int[nChildren];
  210.     int[] yOffsets = new int[nChildren];
  211.     int[] ySpans = new int[nChildren];
  212.  
  213.     // determine the child placements
  214.     Dimension alloc = target.getSize();
  215.     Insets in = target.getInsets();
  216.     alloc.width -= in.left + in.right;
  217.     alloc.height -= in.top + in.bottom;
  218.     SizeRequirements.calculateAlignedPositions(alloc.width, xTotal, 
  219.                            xChildren, xOffsets,
  220.                            xSpans);
  221.     SizeRequirements.calculateAlignedPositions(alloc.height, yTotal,
  222.                            yChildren, yOffsets,
  223.                            ySpans);
  224.  
  225.     // flush changes to the container
  226.     for (int i = 0; i < nChildren; i++) {
  227.         Component c = target.getComponent(i);
  228.         c.setBounds(in.left + xOffsets[i], in.top + yOffsets[i],
  229.             xSpans[i], ySpans[i]);
  230.     }
  231.     }
  232.  
  233.     void checkContainer(Container target) {
  234.     if (this.target != target) {
  235.         throw new AWTError("OverlayLayout can't be shared");
  236.     }
  237.     }
  238.     
  239.     void checkRequests() {
  240.     if (xChildren == null || yChildren == null) {
  241.         // The requests have been invalidated... recalculate
  242.         // the request information.
  243.         int n = target.getComponentCount();
  244.         xChildren = new SizeRequirements[n];
  245.         yChildren = new SizeRequirements[n];
  246.         for (int i = 0; i < n; i++) {
  247.         Component c = target.getComponent(i);
  248.         Dimension min = c.getMinimumSize();
  249.         Dimension typ = c.getPreferredSize();
  250.         Dimension max = c.getMaximumSize();
  251.         xChildren[i] = new SizeRequirements(min.width, typ.width, 
  252.                             max.width, 
  253.                             c.getAlignmentX());
  254.         yChildren[i] = new SizeRequirements(min.height, typ.height, 
  255.                             max.height, 
  256.                             c.getAlignmentY());
  257.         }
  258.         
  259.         xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
  260.         yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
  261.     }
  262.     }
  263.         
  264.     private Container target;
  265.     private SizeRequirements[] xChildren;
  266.     private SizeRequirements[] yChildren;
  267.     private SizeRequirements xTotal;
  268.     private SizeRequirements yTotal;
  269.     
  270. }
  271.  
  272.